//
//  HandAnimation.m
//  safeLady
//
//  Created by hyu on 2016/12/14.
//  Copyright © 2016年 xmy. All rights reserved.
//

#import "HandAnimation.h"

@interface HandAnimation()

@property (assign, nonatomic)BOOL isWeak;

@property (assign, nonatomic)BOOL isUpDown;

@end

@implementation HandAnimation

-(void)awakeFromNib{
    [super awakeFromNib];
    self.isWeak = YES;
    self.isUpDown = YES;
    [self.fingerPrintTime fire];
    [self.animationViewTime fire];
}

-(NSTimer *)fingerPrintTime{
    if (!_fingerPrintTime) {
        _fingerPrintTime = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(fingerPrintAnimationAction) userInfo:nil repeats:YES];
    }
    return _fingerPrintTime;
}

-(NSTimer *)animationViewTime{
    if (!_animationViewTime) {
        _animationViewTime = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(animationViewAnimationAction) userInfo:nil repeats:YES];
    }
    return _animationViewTime;
}

-(void)fingerPrintAnimationAction{
    if (self.isWeak) {
        self.fingerPrintView.alpha -= 0.1;
        if (self.fingerPrintView.alpha <= 0.4) {
            self.isWeak = NO;
        }
    }else{
        self.fingerPrintView.alpha += 0.1;
        if (self.fingerPrintView.alpha >= 1.0) {
            self.isWeak = YES;
        }
    }
}

-(void)animationViewAnimationAction{
    switch (self.fingerDirectionType) {
        case FingerDirectionNone:
            
            break;
        case FingerDirectionUp:
            [self animationViewAnimationActionUp];
            break;
        case FingerDirectionDown:
            [self animationViewAnimationActionDown];
            break;
        case FingerDirectionLeft:
            [self animationViewAnimationActionLeft];
            break;
        case FingerDirectionRight:
            [self animationViewAnimationActionRight];
            break;
            
        default:
            break;
    }
}

-(void)animationViewAnimationActionUp{
    CGRect rect = self.animationView.frame;
    if (self.isUpDown) {
        rect.origin.y -= 2;
        self.animationView.frame = rect;
        if (rect.origin.y <= -20) {
            self.isUpDown = NO;
        }
    }else{
        rect.origin.y = 0;
        self.animationView.frame = rect;
        self.isUpDown = YES;
    }
}

-(void)animationViewAnimationActionDown{
    CGRect rect = self.animationView.frame;
    if (self.isUpDown) {
        rect.origin.y += 2;
        self.animationView.frame = rect;
        if (rect.origin.y >= 20) {
            self.isUpDown = NO;
        }
    }else{
        rect.origin.y = 0;
        self.animationView.frame = rect;
        self.isUpDown = YES;
    }
}

-(void)animationViewAnimationActionLeft{
    CGRect rect = self.animationView.frame;
    if (self.isUpDown) {
        rect.origin.x -= 2;
        self.animationView.frame = rect;
        if (rect.origin.x <= -20) {
            self.isUpDown = NO;
        }
    }else{
        rect.origin.x = 0;
        self.animationView.frame = rect;
        self.isUpDown = YES;
    }
}

-(void)animationViewAnimationActionRight{
    CGRect rect = self.animationView.frame;
    if (self.isUpDown) {
        rect.origin.x += 2;
        self.animationView.frame = rect;
        if (rect.origin.y >= 20) {
            self.isUpDown = NO;
        }
    }else{
        rect.origin.x = 0;
        self.animationView.frame = rect;
        self.isUpDown = YES;
    }
}

-(void)dealloc{
    [self.fingerPrintTime invalidate];
    self.fingerPrintTime = nil;
    [self.animationViewTime invalidate];
    self.animationViewTime = nil;
}

@end
